package com.rrd.itext.xmlBean.layoutBean;

import java.io.File;
import java.io.FileInputStream;

import com.itextpdf.text.Image;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfImportedPage;
import com.itextpdf.text.pdf.PdfReader;
import com.rrd.itext.obj.ResouceObject;
import com.rrd.itext.utils.HttpClientUtil;
import com.rrd.itext.utils.IOUtil;
import com.rrd.itext.utils.PDFBuilder;
import com.rrd.itext.utils.pdf2Image.PDF2Image;
import com.rrd.itext.xmlBean.dataBean.DataXmlZone;
import com.rrd.itext.xmlBean.layoutBean.iface.PdfElementParser;
import com.rrd.itext.xmlBean.layoutBean.iface.PdfElementRender;
import com.rrd.itext.xmlBean.layoutBean.iface.PositionElement;
import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamAsAttribute;


@XStreamAlias("Image")
public class XmlImage implements PdfElementRender, PdfElementParser<Image>,
		PositionElement {
	@XStreamAsAttribute
	private String positionType;
	@XStreamAsAttribute
	private Integer width;
	@XStreamAsAttribute
	private Integer height;
	@XStreamAsAttribute
	private Float offsetX;
	@XStreamAsAttribute
	private Float offsetY;
	@XStreamAsAttribute
	private String name;
	@XStreamAsAttribute
	private String filetype;
	@XStreamAlias("source")
	private String source;
	public String getPositionType() {
		return positionType;
	}

	public void setPositionType(String positionType) {
		this.positionType = positionType;
	}

	public Integer getWidth() {
		return width;
	}

	public void setWidth(Integer width) {
		this.width = width;
	}

	public Integer getHeight() {
		return height;
	}

	public void setHeight(Integer height) {
		this.height = height;
	}

	public Float getOffsetX() {
		return offsetX;
	}

	public void setOffsetX(Float offsetX) {
		this.offsetX = offsetX;
	}

	public Float getOffsetY() {
		return offsetY;
	}

	public void setOffsetY(Float offsetY) {
		this.offsetY = offsetY;
	}

	public String getSource() {
		return source;
	}

	public void setSource(String source) {
		this.source = source;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	public String getFiletype() {
		return filetype==null?"Image":filetype;
	}
	public void setFiletype(String filetype) {
		this.filetype = filetype;
	}

	@Override
	public void render(PdfContentByte cb, PDFBuilder builder) throws Exception {
	    String path = this.parsePath(builder);
	    if(path == null){
	        return;
	    }
        try {
            if("pdf".equalsIgnoreCase(this.getFiletype())){
                byte[] remotePdf = null;
                if (path.startsWith("http")) {
                    remotePdf = HttpClientUtil.getInstence().requestURL(path);
                } else {
                    remotePdf = IOUtil.inputStreamToByte(new FileInputStream(new File(path)));
                }
                if (remotePdf == null) {
                    return;
                }
                PdfReader reader = new PdfReader(remotePdf);
                PdfImportedPage importedPage = builder.getWriter().getImportedPage(reader, 1);
                cb.addTemplate(importedPage, 1f, 0, 0, 1, 0, 0);
            }else{
                Image img = Image.getInstance(path);
                img.scaleAbsolute(this.getWidth(), this.getHeight());
                img.scaleAbsolute(this.getWidth(), this.getHeight());
                img.setAbsolutePosition(this.getOffsetX(), this.getOffsetY());
                cb.addImage(img);
            }
        } catch (Exception e) {
            System.err.println("iTextService Excpetion:" + e.getMessage());
        }
	}

	private String parsePath(PDFBuilder builder) {
		String path = "";
		if (this.getSource() != null && !"".equals(this.getSource())) {
			path = builder.getResPath() + this.getSource();

		} else if (this.getName() != null && !"".equals(this.getName())) {
			if (builder.getResourceMap().containsKey(this.getName())) {
			    //Set file type value from data xml
			    DataXmlZone dataZone = ResouceObject.getDataZone(builder.getResourceMap(), this.getName());
			    this.setFiletype(dataZone.getFiletype());
				path = ResouceObject.getDataContent(builder.getResourceMap(), name);
			}
		}
		if ("".equals(path)) {
			return null;
		}
		if (this.getSource() != null && !IOUtil.isFile(path)) {
			return null;
		}
		return path;
	}

	@Override
	public Image parse(PDFBuilder builder) throws Exception {
		String path = this.parsePath(builder);
        if (this.getFiletype().equalsIgnoreCase("Pdf")) {
            byte[] remotePdf = PDF2Image.getInstance(path, true).toSingleImage();
            Image img = Image.getInstance(remotePdf);
            img.scaleAbsolute(this.getWidth(), this.getHeight());
            return img;
        }else{
            Image img = Image.getInstance(path);
            img.scaleAbsolute(this.getWidth(), this.getHeight());
            return img;
        }
	}

}
